package com.taxsystem.chenliangli.askquestion;

import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.main.taxsystem.R;
import com.taxsystem.chenliangli.askquestion.data.answer;
import com.taxsystem.chenliangli.askquestion_tool.HttpUtil;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class enterActivity extends Activity {
	
	private EditText title,content,tel;
	private TextView tv_alter;
	private RadioGroup group1,group2;
	private Button submit;
	private ImageView alter;
	private int ig=0;//用来记录图片的状态的       
	String[] ans=new String[2];//用来记录提交之后的响应结果的
	private JSONObject jsonanswer=new JSONObject();
	public final static String RECI_COAST = "sw.system.HTTUTIL";
    private String URL= "http://mtaxtest.duapp.com/question/add";
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.entering_question); 
		init();
	}
	
	
	//初始化所有的组件
	public void init()
	{
		
		title=(EditText)findViewById(R.id.enter_tv_title);
		content=(EditText)findViewById(R.id.enter_tv_neiron);
		tel=(EditText)findViewById(R.id.enter_tv_tel);
		//group1=(RadioGroup)findViewById(R.id.entering_radioGroup1);
        //group2=(RadioGroup)findViewById(R.id.entering_radioGroup2);
		alter=(ImageView)findViewById(R.id.enter_iv_alter);
		tv_alter=(TextView)findViewById(R.id.tv_alter);
		alter.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) {
				//记录alter已经被点击了
				if(ig==0)
				{
					alter.setImageResource(R.drawable.open);
					ig=1;
				}
				else
				{
					alter.setImageResource(R.drawable.close);
					ig=0;
				}
				
			}});
		
		submit=(Button)findViewById(R.id.enter_button_submit);
		submit.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) {
				//获得要录入问题的数据
				//将问题提交!!!!!!!!!!!!!!!!!!!!!!!这里还没实现（这里的接口没给出）
				getEnterRequest();
				
				if(ans[1]!=null)
				{
					//提示是否成功提交
					tv_alter.setText(ans[1]);
					//提交之后返回主界面
					Intent intent=new Intent();
					intent.setClass(enterActivity.this, fristpageActivity.class);
					startActivity(intent);
				}
				else
				{
					tv_alter.setText("提交失败");
				}
				
				
			}});
	}
	
	
	
	//获得提交问题的响应
	public  void  getEnterRequest()
	{
		
		
		try {
		
			//注册广播并向服务器端发送请求，将返回的数据放到jsonanseer中     
		      MyBroadcast();
			  ans[0]=jsonanswer.getString("code");
			  ans[1]=jsonanswer.getString("message");
		      
			
			} catch (JSONException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			
			
			
	}
	
	
	
	
	/***注册广播的**/
	/***********注册广播的**********************/
	public void MyBroadcast()
	{
		
		// 注册广播
		IntentFilter filter = new IntentFilter(RECI_COAST);
		BroadcastReceiver myrecive = new MyRecive();
		registerReceiver(myrecive, filter);
		Thread thread = new Thread() 
		{

			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST);
				//获取服务器返回的信息
				String reslut = HttpUtil
						.postRequest(URL,"1",title.getText().toString(),content.getText().toString(),"1");
				intent.putExtra("data", reslut);
				//发送广播
				sendBroadcast(intent);
			}

		};
		
	}
	
	// 定义BroadcastReceiver 接收返回的信息
	private class MyRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				//获取JSONObject对象
				jsonanswer = new JSONObject(
						intent.getStringExtra("data"));
				
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}
	
	
	
	

}
